var newsEditor = null;
EditNews = function() {
	K = KindEditor;
	return {
		init : function() {
			EditNews.initKindEditor();
		},
		
		initKindEditor : function() {
			newsEditor = K.create('#editor_id', {
				allowImageUpload	: true,
				uploadJson			: basePath + '/pc/news/uploadImage.htm',
				filePostName		: 'imgFile',
				basePath            : basePath + '/resources/js/common/kindeditor/'
			});
		},
		
		submitForm : function(state) {
			if(!$('#newsForm').form('validate')) {
				return;
			}
			newsEditor.sync();
			if(newsEditor.isEmpty()){
				Ict.warn("请输入新闻内容!");
				return;
			}
			$("#state").val(state);
			var title = $.trim($("#id_title").val());
			var newsId = $("#id_newsId").val();
			$.post('/pc/news/check.htm', {'title' : title, 'newsId' : newsId}, function(data) {
				if(data.code == '0') {
					Ict.confirm("新闻标题已存在,是否继续?", function(yes) {
						if(yes) {
							EditNews.saveNewsInfo();
						}
					});
				} else if(data.code == '1') {
					EditNews.saveNewsInfo();
				} else {
					Ict.warn("未知错误");
				}
			});
		},
		
		saveNewsInfo : function() {
			$('#newsForm').form('submit', {
				url      : "/pc/news/saveNewsInfo.htm",
				onSubmit : function() {
					return $('#newsForm').form('validate');
				},
				success  : function(data) {
					if(data == "SUCCESS") {
						Ict.info("保存成功!", function() {
							Ict.closeWin2();
							parent.$("iframe").attr('src', "/pc/news/main.htm");
			    		});
					} else {
						Ict.info("保存失败");
					}
				}
			});
		}
	};
}();

$(function(){
	EditNews.init();
});